package com.example.controller;

import com.example.po.Student;
import com.example.service.StudentService;
import com.example.utils.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class StudentController {
    @Autowired
    private StudentService studentService;

    /**
     * 通过学号和姓名查询学生信息
     * @param student
     * @param session
     * @return
     */
    @RequestMapping("/search")
    @ResponseBody
    public List<Student> searchStudent(Student student, HttpSession session){
        List<Student> students=studentService.findStudents(student,session);
        return students;
    }
    /**
     * 判断学生信息是否存在
     * @param student
     * @param session
     * @return
     */
    @RequestMapping("/searchById")
    @ResponseBody
    public String searchStudentById(Student student, HttpSession session){
        List<Student> students=studentService.searchStudentById(student);
        if(students.size()==0)return "0";
        else return "1";
    }

    /**
     * 查询学生信息
     * @param student
     * @param session
     * @return
     */
    @RequestMapping("/searchAStudent")
    @ResponseBody
    public List<Student> searchAStudent(Student student, HttpSession session){
        List<Student> students=studentService.searchStudentById(student);
        return students;
    }

    /**
     * 修改学生信息
     * @param student
     * @param session
     * @return
     */
    @RequestMapping("/updateStudentInfo")
    @ResponseBody
    public String updateStudent(Student student, HttpSession session){
        if(studentService.updateStudent(student)){
            return "修改成功";
        }else{
            return "修改失败";
        }
    }
    /**
     * 获取页面信息
     * @param session
     * @return
     */
    @RequestMapping("/getPageInfo")
    @ResponseBody
    public Page getPasgeInfo(HttpSession session){
       Page page= (Page) session.getAttribute("page");
        return page;
    }

    /**
     * 设置当前页面
     * @param session
     */
    @RequestMapping("/setPageInfo")
    @ResponseBody
    public String setPasgeInfo(int idx,HttpSession session){
        Page page= (Page) session.getAttribute("page");
        page.setStart(idx);
        session.setAttribute("page",page);
        //System.out.println(session.getAttribute("page"));
        return "1";
    }

    /**
     * 添加学生信息
     * @param student
     * @return
     */
    @RequestMapping("/addStudents")
    @ResponseBody
    public String addStudent(Student student){
        System.out.println(student);
        String str = studentService.addStudent(student);
        if(str=="error"){
            return "error";
        }else if(str=="OK"){
            return "OK";
        }else{
            return "fail";
        }
    }
    @RequestMapping("/delete")
    @ResponseBody
    public String DeleteStudent(int id){
        System.out.println(id);
        if(studentService.deleteStudent(id)){
            return "删除成功";//删除成功
        }else{
            return "删除失败";//删除失败
        }
    }
    //浏览所有的学生信息
    @RequestMapping("/searchStudents")
    @ResponseBody
    public List<Student> searchStudents(){
        List<Student> students = studentService.searchStudent();
        return students;
    }
}
